package cn.hyp.bantang.module.finance.service.basexj;

import cn.hyp.bantang.framework.common.pojo.PageResult;
import cn.hyp.bantang.framework.common.util.object.BeanUtils;
import cn.hyp.bantang.module.finance.controller.admin.basexj.vo.BaseXjPageReqVO;
import cn.hyp.bantang.module.finance.controller.admin.basexj.vo.BaseXjSaveReqVO;
import cn.hyp.bantang.module.finance.dal.dataobject.basexj.BaseXjDO;
import cn.hyp.bantang.module.finance.dal.mysql.basexj.BaseXjMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.*;

import static cn.hyp.bantang.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.hyp.bantang.module.finance.enums.ErrorCodeConstants.BASE_XJ_NOT_EXISTS;

/**
 * 个股报告期现金 Service 实现类
 *
 * @author 半唐
 */
@Service
@Validated
public class BaseXjServiceImpl implements BaseXjService {

    @Resource
    private BaseXjMapper baseXjMapper;

    @Override
    public Long createBaseXj(BaseXjSaveReqVO createReqVO) {
        // 插入
        BaseXjDO baseXj = BeanUtils.toBean(createReqVO, BaseXjDO.class);
        baseXjMapper.insert(baseXj);
        // 返回
        return baseXj.getId();
    }

    @Override
    public void updateBaseXj(BaseXjSaveReqVO updateReqVO) {
        // 校验存在
        validateBaseXjExists(updateReqVO.getId());
        // 更新
        BaseXjDO updateObj = BeanUtils.toBean(updateReqVO, BaseXjDO.class);
        baseXjMapper.updateById(updateObj);
    }

    @Override
    public void deleteBaseXj(Long id) {
        // 校验存在
        validateBaseXjExists(id);
        // 删除
        baseXjMapper.deleteById(id);
    }

    private void validateBaseXjExists(Long id) {
        if (baseXjMapper.selectById(id) == null) {
            throw exception(BASE_XJ_NOT_EXISTS);
        }
    }

    @Override
    public BaseXjDO getBaseXj(Long id) {
        return baseXjMapper.selectById(id);
    }

    @Override
    public PageResult<BaseXjDO> getBaseXjPage(BaseXjPageReqVO pageReqVO) {
        return baseXjMapper.selectPage(pageReqVO);
    }

}